//存放主要交互逻辑的js代码
// javascript 模块化(package.类.方法)

var seckill = {

    //封装秒杀相关ajax的url
    URL: {
        now: function () {
            return '/seckill/time/now';
        },
        exposer: function (seckillId) {
            return '/seckill/' + seckillId + '/exposer';
        },
        execution: function (seckillId, md5) {
            return '/seckill/' + seckillId + '/' + md5 + '/execution';
        }
    },

    //验证手机号
    validatePhone: function (phone) {
        if (phone && phone.length == 11 && !isNaN(phone)) {
            return true;//直接判断对象会看对象是否为空,空就是undefine就是false; isNaN 非数字返回true
        } else {
            return false;
        }
    },
    countdown:function (seckillId,nowTime,endTime,startTime) {
            //时间判断
        // console.log(nowTime+" "+startTime+" " + endTime);

        var seckillBox=$('#seckill-box');
        if (nowTime>endTime){
            //秒杀结束
            seckillBox.html('秒杀结束！');
        }else if (nowTime<startTime){
            var killTime=new Date(startTime+1000);
            seckillBox.countdown(killTime,function (event) {
                var format=event.strftime('秒杀计时：%D天 %H时 %M分 %S秒');
                seckillBox.html(format);

            }).on('finish.countdown',function () {
                //获取秒杀地址，控制现实逻辑，执行秒杀
                seckill.hendleSeckillkill(seckillId,seckillBox);
            });
        }else {
            seckill.hendleSeckillkill(seckillId,seckillBox);


        }
    },
        hendleSeckillkill:function (seckillId,node) {


          //处理秒杀逻辑
            node.hide().html('<button id="killBtn">开始秒杀</button>')
            $.post(seckill.URL.exposer(seckillId),{},function (result) {

                if (result && result['success']){
                    var exposer=result['data'];
                    if(exposer['exposed']){
                        //开始秒杀
                        var md5=exposer['md5'];
                        //获取秒杀地址
                        var userPhone = $.cookie('userPhone');
                        var killUrl=seckill.URL.execution(seckillId,md5);
                        console.log('killUrl:'+killUrl);
                        //绑定一次点击事件
                        $('#killBtn').one('click',function () {

                            //执行秒杀请求
                            //1:禁用按钮
                            $(this).addClass('disabled');
                            //2:发送秒杀请求执行秒杀
                            $.post(killUrl,{},function (result) {

                               if (result){
                                   var killResult=result['data'];
                                   var state=killResult['state'];
                                   var stateInfo=killResult['stateInfo'];
                                   //3:显示秒杀结果
                                   node.html('<span class="label">'+stateInfo+'</span>');

                               }

                            });

                        });
                        node.show(300);
                    }else {
                        //未开启秒杀,计时偏差
                        var  now=exposer['now'];
                        var  start=exposer['start'];
                        var  end=exposer['end'];
                        seckill.countdown(seckillId,now,start,end);
                    }

                }else {
                    console.log('result:'+result);
                }
            });
        },

    //详情页秒杀逻辑
    detail: {
        //详情页初始化
        init: function (params) {
            //手机验证和登录,计时交互
            //规划我们的交互流程
            //在cookie中查找手机号
            var userPhone = $.cookie('userPhone');
            //验证手机号
            if (!seckill.validatePhone(userPhone)) {
                //绑定手机 控制输出
                var killPhoneModal = $('#killPhoneModal');
                killPhoneModal.modal({
                    show: true,//显示弹出层
                    backdrop: 'static',//禁止位置关闭
                    keyboard: false//关闭键盘事件
                });

                $('#killPhoneBtn').click(function () {
                    var inputPhone = $('#killPhoneKey').val();
                  //  console.log("inputPhone: " + inputPhone);
                   // alert("点击了");
                    if (seckill.validatePhone(inputPhone)) {
                        //电话写入cookie(7天过期)
                        $.cookie('userPhone', inputPhone, {expires: 7, path: '/seckill'});
                        //验证通过　　刷新页面
                        window.location.reload();

                    } else {
                        //todo 错误文案信息抽取到前端字典里
                        $('#killPhoneMessage').hide().html('<label class="label label-danger"><font color="#FF0000">手机号错误!</font></label>').show(300);
                    }
                });
            }
            var seckillId=params['seckillId'];
            var endTime=params['endTime'];
            var startTime=params['startTime'];

            $.get(seckill.URL.now(),{},function (result) {
                // print(result['success']);

                if (result && result['success']){
                    var nowTime=result['data'];
                    //时间判断,
                    seckill.countdown(seckillId,nowTime,endTime,startTime);
                }
            });

        }
    }

}